﻿/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;

	public class Control extends MovieClip
	{
		public static const VISIBLE:String = "visible";
		public static const ENABLED:String = "enabled";
		public static const RESIZED:String = "resized";
		
		public function Control()
		{
			super();
		
			controlWidth  = super.width;
			controlHeight = super.height;
		
			Init();
			
		}
		
		protected function Init():void
		{
			if( this.hasOwnProperty("init") )
			{
				var fInit:Function = this["init"] as Function;
				if( fInit != null ) fInit();
			}	
		}
		
		public override function set visible(value:Boolean):void
		{
			super.visible = value;
			dispatchEvent(new Event(VISIBLE));
		}
				
		public override function set enabled(value:Boolean):void
		{
			if( super.enabled != value )
			{
				super.enabled = value;
				dispatchEvent(new Event(ENABLED));
			}
		}
		
		private var requireLayout:Boolean = false;
		public function RequireLayout():void
		{
			if( requireLayout == true ) return;
			requireLayout = true;
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);
		}
		
		private var requireDelayedLayout:Boolean = false;
		public function RequireDelayedLayout():void
		{
			if( requireDelayedLayout == true ) return;
			requireDelayedLayout = true;
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);
		}
		
		private function OnEnterFrame(e:Event):void
		{
			if( requireDelayedLayout == true )
			{
				requireDelayedLayout = false;
			}
			
			else
			{
				removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
				requireLayout = false;
				Layout();
			}
		}
		
		protected function Layout():void {}
		
		private var controlWidth:Number;
		private var controlHeight:Number;
		private var noscale:Boolean = false;
		protected function set NoScale(value:Boolean):void
		{
			noscale = value;
		}
		
		public override function set width(value:Number):void
		{
			controlWidth = value;
			if( noscale ) RequireLayout();
			else 		  super.width = value;
			
			if( hasEventListener(RESIZED) ) dispatchEvent(new Event(RESIZED));
		}
		
		public override function set height(value:Number):void
		{
			controlHeight = value;
			if( noscale ) RequireLayout();
			else 		  super.height = value;
			
			if( hasEventListener(RESIZED) ) dispatchEvent(new Event(RESIZED));
		}
		
		public override function get width():Number { return noscale ? controlWidth : super.width; }
		public override function get height():Number { return noscale ? controlHeight : super.height; }
		
		public override function set scaleX(value:Number):void
		{
			if( noscale ) { width *= value; RequireLayout(); }
			else super.scaleX = value;
			
			if( hasEventListener(RESIZED) ) dispatchEvent(new Event(RESIZED));
		}
		
		public override function set scaleY(value:Number):void
		{
			if( noscale ) {height *= value; RequireLayout(); }
			else super.scaleY = value;
			
			if( hasEventListener(RESIZED) ) dispatchEvent(new Event(RESIZED));
		}
		
		public function FindParent(classname:String):*
		{
			var p:DisplayObjectContainer = parent;
			while(p != null )
			{
				if( getQualifiedClassName(p) == classname ) return p;
				p = p.parent;
			}
			
			return null;
		}
	}
}












